

package com.hazelcast.client.impl.clientside;

import com.hazelcast.core.HazelcastInstanceAware;
import com.hazelcast.core.ManagedContext;
import com.hazelcast.internal.serialization.SerializationServiceAware;

final class HazelcastClientManagedContext implements ManagedContext {

    private final HazelcastClientInstanceImpl instance;
    private final ManagedContext externalContext;
    private final boolean hasExternalContext;

    HazelcastClientManagedContext(HazelcastClientInstanceImpl instance, ManagedContext externalContext) {
        this.instance = instance;
        this.externalContext = externalContext;
        this.hasExternalContext = externalContext != null;
    }

    @Override
    public Object initialize(Object obj) {
        Object object = obj;
        if (object instanceof HazelcastInstanceAware aware) {
            aware.setHazelcastInstance(instance);
        }
        if (object instanceof SerializationServiceAware aware) {
            aware.setSerializationService(instance.getSerializationService());
        }

        if (hasExternalContext) {
            object = externalContext.initialize(object);
        }
        return object;
    }
}
